from problembase import ProblemBase
from dolfin import RectangleMesh, Expression
from math import sqrt

class DrivenSquare(ProblemBase):
    'Sqrt(side) square driven by a constant velocity field (v1,v2).'
    def __init__(self,options):
        ProblemBase.__init__(self,options)

        self.LL = (-2.,-2.)                              # lower left
        self.UR = (6.,2.)                                # upper right; points for rectangle defition

        self.domain = RectangleMesh(self.LL[0],self.LL[1],self.UR[0],self.UR[1],2*self.N,1*self.N)
        
        self.side = sqrt(2)                         # length of the square side
        self.vStrings = ('1.','0.')                  # string for velocity compononets    
        self.vIsTimeDependent = False               
        self.hasExactSolution = True                # exact is defined
        self.phi_ = self.exact_solution() 
        self.T = 4        
        self.activeCols = [0,1,2,3,4,5,6,7]
        ProblemBase.register_variables(self)
        self.ibcExpr = self.exact_solution
        self.L = 4*self.side

    def exact_volume(self):
        '''Return problem specific exact volume.'''
        return self.side**2
        
    def exact_solution(self,t=0):
        '''Return problem specific exact solution at time t.'''
        hslant = sqrt(self.exact_volume()/2.) 
        return Expression('fabs(x[0]-(%(vX)s)*t)+fabs(x[1]-(%(vY)s)*t)-hslant' % {'vX' : self.vStrings[0],'vY' : self.vStrings[1]},
                                                                                  hslant=hslant,t=t)








